package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.entity.CementingSiteDesign;
import com.b511.swpu.cementbackend.repository.CementingSiteDesignRepository;
import com.b511.swpu.cementbackend.service.CementingSiteDesignService;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Api(value = "固井现场设计", tags = {"固井现场设计"})
@RestController
@RequestMapping("/cementing_site_design")
public class CementingSiteDesignController {
    private final CementingSiteDesignService cementingSiteDesignService;
    private final CementingSiteDesignRepository cementingSiteDesignRepository;

    public CementingSiteDesignController(CementingSiteDesignService cementingSiteDesignService,
                                         CementingSiteDesignRepository cementingSiteDesignRepository) {
        this.cementingSiteDesignService = cementingSiteDesignService;
        this.cementingSiteDesignRepository = cementingSiteDesignRepository;
    }

    @GetMapping("/{num}/{size}")
    public R getCementingSiteDesignByCondition(@PathVariable int num,
                                             @PathVariable int size) {
        return cementingSiteDesignService.getCementingSiteDesign( PageRequest.of(num, size));
    }

    @PutMapping("/{id}")
    public R updateCementingSiteDesign(@PathVariable Long id, @RequestBody CementingSiteDesign updateCementingSiteDesign) {
        return cementingSiteDesignService.updateCementingSiteDesign(id, updateCementingSiteDesign);
    }

    @DeleteMapping("/{id}")
    @Transactional
    public R deleteCementingSiteDesignByCondition(@PathVariable Long id) {
        cementingSiteDesignRepository.deleteById(id);
        return R.ok();
    }
}
